<?php
App::uses('AppModel', 'Model');
/**
 * Article Model
 *
 * @property Menuses $Menuses
 * @property Domain $Domain
 * @property Post $Post
 */
class Article extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'name';
	
	public $virtualFields = array(
			'url' => 'CONCAT("/a/", Article.slug)'
	);
	
	public $actsAs = array('Search.Searchable');
	
	public $filterArgs = array(
		'filter' => array('type' => 'query', 'method' => 'orConditions'),
		'status' => array('type' => 'value'),
		'domain_id' => array('type' => 'value')
	);
	
	
	
	
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'menuses_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'domain_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */

	public $belongsTo = array(
			'Menus' => array(
				'className' => 'Menus',
				'foreignKey' => 'menuses_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
	),
	
			'Domain' => array(
				'className' => 'Domain',
				'foreignKey' => 'domain_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
	),
			'User' => array(
				'className' => 'User',
				'foreignKey' => 'user_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
	)
	);
	
	public function afterSave( $options = array() ) {
		if(isset($this->data['Article']['domain_id'])){
			$this->Domain->id = $this->data['Article']['domain_id'];
			$this->Domain->saveField('cache',1);
		}
		return true;
	}
	
/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Post' => array(
			'className' => 'Post',
			'joinTable' => 'posts_articles',
			'foreignKey' => 'article_id',
			'associationForeignKey' => 'post_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		),
		'Upload' => array(
					'className' => 'Upload',
					'joinTable' => 'articles_uploads',
					'foreignKey' => 'article_id',
					'associationForeignKey' => 'upload_id',
					'unique' => 'keepExisting',
					'conditions' => '',
					'fields' => '',
					'order' => '',
					'limit' => '',
					'offset' => '',
					'finderQuery' => '',
					'deleteQuery' => '',
					'insertQuery' => ''
		),
		'Rating' => array(
			'className' => 'Rating',
			'joinTable' => 'rating_articles',
			'foreignKey' => 'article_id',
			'associationForeignKey' => 'rating_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		),
		'Comment' => array(
			'className' => 'Comment',
			'joinTable' => 'articles_comments',
			'foreignKey' => 'article_id',
			'associationForeignKey' => 'comment_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
	
	public function orConditions($data = array()) {
		$filter = $data['filter'];
		$cond = array(
				'OR' => array(
						$this->alias . '.name LIKE' => '%' . $filter . '%',
						$this->alias . '.content LIKE' => '%' . $filter . '%',
				));
		return $cond;
	}

}
